﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.IoC;
using Kooboo.Domain;
using System.Data;

namespace Kooboo.Commerce.Contexts.Customers
{
    public static class EditorBehavior
    {
        #region Customer
        public static void Post(this IEditor actor, ICustomer customer, string password)
        {

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {

                    if (string.IsNullOrWhiteSpace(customer.UserName))
                    {
                        throw new ArgumentNullException("Customer.UserName");
                    }

                    var repository = ContextContainer.Current.Resolve<ICustomerRepository>();

                    if (repository.Customers.Any(i => i.UserName == customer.UserName))
                    {
                        throw new DuplicateNameException(customer.UserName);
                    }

                    var find = Kooboo.Connect.UserServices.FindUser(customer.UserName);
                    if (find == null)
                    {
                        var status = Kooboo.Connect.UserServices.CreateUser(customer.UserName, password, customer.MailAddress);

                        if (status != Connect.UserCreateStatus.Success)
                        {
                            throw new InvalidOperationException(status.ToString());
                        }

                    }
                    customer.CreateDate = DateTime.Now;
                    repository.Post(customer);
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }

        public static void Put(this IEditor actor, ICustomer customer)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(customer.UserName))
                    {
                        throw new ArgumentNullException("Customer.UserName");
                    }
                    var repository = ContextContainer.Current.Resolve<ICustomerRepository>();
                    if (repository.Customers.Any(i => i.UserName == customer.UserName && i.Id != customer.Id))
                    {
                        throw new DuplicateNameException(customer.UserName);
                    }

                    var user = Kooboo.Connect.UserServices.FindUser(customer.UserName);
                    if (user == null)
                    {
                        throw new KeyNotFoundException(customer.UserName);
                    }


                    var find = repository.Customers
                        .Where(i => i.Id == customer.Id)
                        .FirstOrDefault();

                    find.Birthday = customer.Birthday;
                    find.City = customer.City;
                    find.Country = customer.Country;
                    find.FirstName = customer.FirstName;
                    find.Gender = customer.Gender;
                    find.LastName = customer.LastName;
                    find.MailAddress = customer.MailAddress;
                    find.MiddleName = customer.MiddleName;
                    find.Mobile = customer.Mobile;
                    find.SavingPoint = customer.SavingPoint;
                    find.StarLevel = customer.StarLevel;
                    find.Telphone = customer.Telphone;
                    find.UserName = customer.UserName;

                    repository.Put(find);
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }

        public static void Delete(this IEditor actor, ICustomer customer)
        {

            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<ICustomerRepository>();
                    repository.Delete(customer);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        } 
        #endregion

        #region Address
        public static void Post(this IEditor actor, ICustomer customer, IAddress address)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<IAddressRepository>();
                    repository.Post(customer.Id, address);
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }

        public static void Put(this IEditor actor, ICustomer customer, IAddress address)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<IAddressRepository>();
                    repository.Put(customer.Id, address);
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }

        public static void Delete(this IEditor actor, ICustomer customer, IAddress address)
        {
            using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
            {
                try
                {
                    var repository = ContextContainer.Current.Resolve<IAddressRepository>();
                    repository.Delete(customer.Id, address);
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }

        }  


        #endregion
    }
}
